var publishAccount = {
		editType : 0,	
		tempUser : null,
		
	load : function () {
		Ext.QuickTips.init();
		this.eventRegister();
		$('#addr_1').twzipcode({
			countyName: "county",
			districtName: "area",
			zipcodeNameR: "zipcode"
		}); 
		var qs =  new QueryString();
		publishAccount.editType  = qs.get("editType");
		if (publishAccount.editType == 0 ) { //編輯自己帳號
			var user = getUser();
			publishAccount.tempUser = user;	
			this.setFromValue(user);			
		}  else if (publishAccount.editType ==1){ //編輯 會員資料
			var account =qs.get("account"); //"koko0809@msn.com";//qs.get("customAccount");
			Ext.Ajax.request({
				url : "/ad/sys/com/control/UserService.php?action=getUserNotStatus&account="+account,			
				callback  : function (op, su, rsp) {
					var user =  rsp.responseText.toJSON();
					publishAccount.tempUser = user;	
					publishAccount.setFromValue(user);
				}
			});
		}	 
	},
	setFromValue  : function (user) {
		Ext.getDom("account").value = user.Account;
		Ext.getDom("name").value = user.Name;
		Ext.getDom("phone").value = user.Phone;
		//Ext.getDom("mobile").value = user.mobile;
		Ext.getDom("companyName").value = user.CompanyName;
		
		Ext.getDom("password").value = user.Password;
		Ext.getDom("againPassword").value = user.Password;
			Ext.getDom("address").value = user.Address;
		if ( user.logo != "" &&  user.logo != null) {
			Ext.getDom("preUpLoadImg").src = user.logo;
			Ext.getDom("logo").value = user.logo;
		}
		
		$('#addr_1').twzipcode({
			countyName: "county",
			districtName: "area",
			zipcodeName: "zipcode",
			countySel: user.County, 
			districtSel: user.Area,  
			zipcodeSel : user.Zipcode
		});
		
		
		//  ,"Password":"1234","Phone":"0973011400","CompanyName":"test","Type":"1","Count":"122","ProxyAccount":"","CreateTime":"2011-09-14 21:48:14","ModifyTime":"2011-07-26 16:43:29","logo":null,"Status":"1","County":"\u9ad8\u96c4\u5e02","Area":"\u65b0\u8208\u5340","Address":"test","Zipcode":"800","NotifyBudgetUpdate":"1","NotifyAdApprovedNot":"1","NotifyAdModify":"0","NotifyAdApproved":"0","NotifyPayComplete":"1","BankCode":null,"BankName":null,"BranchOfBank":null,"BankAccount":null,"NotifyTime":null}'.toJSON();
	},
	 
	eventRegister : function () {
		Ext.get("confirm").on('click',this.confrim);  //確認,新增使用者		
		Ext.get("btnCheckAccount").on("click",this.checkAccount); //檢查使用者帳號
	},
	
	
	checkAccount: function (){
		var account = Ext.getDom("account").value;
		
		if (account == "") {
			alert("請輸入[帳號]!");
			return;
		}
		
		if (!account.isEmail()) {
			alert("帳號非Email格式!");
			return;
		}
	 	 
		Ext.get("loading").show();
		 
		Ext.Ajax.request({
			url : "/ad/sys/com/control/UserService.php?action=checkAccount&account="+account,
			callback : function (options, success, rsp) {
				Ext.get("loading").hide();
				var obj = rsp.responseText.toJSON();  
				if (obj.result > 0) {
					alert("帳號已被註冊!");
				}  else {
					alert("帳號未被註冊!");
				}
			}
		});
		 
	},
	
	confrim : function () {	   	 
	    
		if (!Ext.getDom("account").value){
	    	alert("請輸入[帳號]!");
	    	Ext.getDom("account").focus();
	    	return;
	    }
		
		if (!Ext.getDom("account").value.isEmail()){
	    	alert("[帳號]非正確Email格式!");
	    	Ext.getDom("account").focus();
	    	return;
	    }
	    
	    if (!Ext.getDom("name").value) {
	    	alert("請輸入[姓名]!");
	    	Ext.getDom("name").focus();
	    	return;
	    }
	   
	    if (!Ext.getDom("phone").value.isPhone()) {
	    	alert("[電話]非正確電話格式!");
	    	Ext.getDom("phone").focus();
	    	return;
	    }  
	     
	    if (!Ext.getDom("companyName").value) {
	    	alert("請輸入[公司名稱]!");
	    	Ext.getDom("companyName").focus();
	    	return;
	    }  
	     
	   
	    var county =  getSelectValue(document.getElementsByName("county")[0].options);
	    
	     
	    if (!county) {
	    	alert("請選擇[縣市]!");
	    	document.getElementsByName("county")[0].focus();
	    	return ;
	    }
	    var area = getSelectValue(document.getElementsByName("area")[0].options);
	    var zipcode = document.getElementsByName("zipcode")[0].value;
	    
	    if (!Ext.getDom("address").value) {
	    	alert("請輸入[住址]!")
	    	Ext.getDom("address").focus();
	    	return;
	    }
	    
	    var password = Ext.getDom("password").value;
	    var againPassword = Ext.getDom("againPassword").value;
	    
	    if (!password) {
	    	alert("請輸入[密碼]!");
	    	Ext.getDom("password").focus();
	    	return ;
	    }
	    
	    if (password != againPassword) {
	    	alert("[密碼]輸入不一致");
	    	Ext.getDom("againPassword").focus();
	    	return;
	    }
	  
	    waitShow("新增中...");
	   
	    Ext.Ajax.request({
	    	method : "POST",
	     	form : "userForm",
	    	callback : function (options, se, rsp) {
	    		waitHide();
	    		var obj = rsp.responseText.toJSON();  
				//alert(rsp.responseText);

	    		if (obj.result =="1") {
	    			alert("新增成功!");
	    			window.location='publishMAppList.php';
	    		} else{
	    			alert(obj.msg);
	    		}
	    	}
	    	
	    });
	    //document.getElementById("userForm").submit();
 	}  
};

Ext.onReady(function (){
	publishAccount.load();
});


